/**
 * DataUtil.java
 * com.ebook.util
 * Copyright (c) 2013, 公司版权所有.
*/

package com.ebook.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.SecureRandom;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ebook.core.domain.Note;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 工具类
 * @author    潘吉友
 * @Date	 2013-3-11 	 
 */
public class DataUtil {
	
	private static Log logger = LogFactory.getLog(DataUtil.class); 

	public static String objToStr(String s) {
		if(s == null || s == ""){
			return "";
		}
		else{
			return s.trim();
		}		
	}
	
	/**
	 * 随机生成6位字符串
	 * @return 字符串
	*/
	public static String randomCheckCode(){
		int length = 6;
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		for (int i = 0; i < length; i++) {
			num = num * 10;
		}
		String password = ((int) ((random * num)) + "").trim();
		return password;
	}
	
	/**
	 * 核对两个字符串是否相等
	 * @param str1
	 * @param str2
	 * @return true:相等；false:不相等
	*/
	public static boolean checkString(String str1,String str2){
		if(str1==null || str1 == ""|| str2 ==null || str2==""){
			return false;
		}
		if(str1.equals(str2)){
			return true;
		}else{
			return false;
		}
	}
	
private static final String POSSIBLE_CHARS="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";   
	
	public static String generateRandomString(int length) {   
        StringBuilder sb = new StringBuilder(length);   
        SecureRandom random = new SecureRandom();   
        for (int i = 0; i < length; i++) {   
            sb.append(POSSIBLE_CHARS.charAt(random.nextInt(POSSIBLE_CHARS.length())));   
        }   
        return sb.toString();   
    }   
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object mapToObject(Map<String, Object> map, Class clazz) {
		try{
			 Object objectCopy = clazz.newInstance();
		        Set entrySet = map.entrySet();
		        for (Iterator it = entrySet.iterator(); it.hasNext();) {
		            Entry entry = (Entry) it.next();
		            Object key = entry.getKey();
		            Object value = entry.getValue();
		            System.out.println(key + "\t" + value);
		            Field field = clazz.getDeclaredField(String.valueOf(key));
		            String fieldName = field.getName();
		            // 获得和属性对应的getXXX()方法的名字
		            StringBuilder getMethodName = new StringBuilder("get");
		            getMethodName.append(fieldName.substring(0, 1).toUpperCase()).append(fieldName.substring(1));
		            // 获得和属性对应的setXXX()方法的名字
		            StringBuilder setMethodName = new StringBuilder("set");
		            setMethodName.append(fieldName.substring(0, 1).toUpperCase()).append(fieldName.substring(1));
		            // 获得和属性对应的setXXX()方法
		            Method setMethod = clazz.getMethod(setMethodName.toString(), new Class[] { field.getType() });
		            // 调用拷贝对象的setXXX()方法
		            setMethod.invoke(objectCopy, new Object[] { value });
		        }
		        return objectCopy;
		}catch(Exception e){
			logger.error("反射创建对象失败！"+e);
			return null;
		}   
    }
	
	public static void main(String[] args) {
//		Double d1 = 1.0;
//		System.out.println(d1.intValue());
		String noteString = "[{\"contain_file\":\"1111111111\",\"note_index\":\"50\",\"noteid\":\"20130609155639398000\",\"startCFI\":\"epubcfi(/6/44!/5/2/2/1:200)\","+
"\"endCFI\":\"epubcfi(/6/44!/5/2/5/1:200)\",\"select_content\":\"bbbbbbbbbbbbbbbbb\",\"note_content\":\"bbbbbbbbbbbbbbbbb\","+
"\"isNote\":\"0\",\"color\":\"color\",\"div_status\":\"div_status\",\"issynronezed\":\"1\","+
"\"chapter_title\":\"chapter_title\",\"curr_chapter\":\"21\",\"bookid\":\"1\",\"uid\":\"0\",\"create_time\":\"2013-06-09 15:56:39.398\","+
"\"update_time\":\"2013-06-09 15:56:39.398\"}]"; 
		Gson gson = new Gson();
		List<Note> list = gson.fromJson(noteString, new TypeToken<List<Note>>() {}.getType());
		for(Note n : list){
			System.out.println(n.getBookid());
			
		}
		
		
		
		
	}
}

